package com.gitxinghui;

import cn.hutool.core.util.ReUtil;
import cn.hutool.http.HttpUtil;
import com.gitxinghui.constants.GitHubUrl;

import java.util.ArrayList;
import java.util.concurrent.locks.ReentrantLock;

public class GetIpThree implements Runnable {

    private int num = 0;

    private String[] arr = GitHubUrl.GITHUB_URL;

    private final Object lock = new Object();

    @Override
    public void run() {

        synchronized (lock) {
            while(true) {
                if (num < arr.length) {
                    String url = GitHubUrl.SELECT_IP_ADDRESS_URL + arr[num];
//                    System.out.println(url);
                String content = HttpUtil.get(url);

                ArrayList<String> all = ReUtil.findAll(GitHubUrl.IP_PATTERN, content, 0, new ArrayList<>());

                System.out.println(Thread.currentThread().getName()+":"+all.get(0));

                    num++;


                    try {
                        lock.wait(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }else{
                    break;
                }
            }
        }

    }
}
